package com.touch.sound;
import java.net.SocketException;

import com.touch.core.net.NetListener;
import com.touch.core.net.UDPServer;

import javax.swing.*;
public class MediaServer implements NetListener
{
	protected UDPServer server;
	protected MediaPlayer mediaplayer = new MediaPlayer();
	protected JFrame frame = new JFrame("SE Media Server");
	protected JTextArea text = new JTextArea();
	public MediaServer() throws SocketException
	{		
		this.server  = new UDPServer(32500);
		server.addNetListener(this);
		this.frame.add(new JScrollPane(text));
		this.frame.setSize(300, 200);
		this.frame.setVisible(true);
	}	
	public static void main(String args[])
	{
		try
		{
			new MediaServer();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			System.exit(0);
		}
	}
	@Override
	public void messageReceived(Object host, byte[] message)
	{
		String packet = new String(message);
		text.setText(packet + "\n" + text.getText());
		if(packet.startsWith("play:"))
		{
			packet = packet.substring(5, packet.length());
			mediaplayer.loop(packet);
		}
		else if(packet.startsWith("stop"))
		{
			packet = packet.substring(4, packet.length());
			mediaplayer.stop();
		}
		else if(packet.startsWith("sfx:"))
		{
			packet = packet.substring(4, packet.length());
			new WavPlayer(packet).start();			
		}
		else if(packet.startsWith("close"))
		{
			packet = packet.substring(5, packet.length());
			System.exit(0);
		}
		
	}
}
